<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Moto_caracteristica_model extends CI_Model {

    //const TABLE = 'moto';

    function __construct() {
        parent::__construct();
        $this->load->config('parametros');
    }

    function getCaracteristicaValor($_id) {
        $this->db->where('id', $_id);
        $query = $this->db->get($this->config->item('dbCliente') . $this->config->item('tblCaracteristicaValor'));

        //pre('query: ' . $this->db->last_query());

        if ($query->num_rows() > 0) {
            return $query->row();
        } else {
            return null;
        }
    }

    /**
     * Get user record by Id
     *
     * @param	int
     * @param	bool
     * @return	object
     */
    function getCaracteristicasValor($_id) {
        $this->db->where('marca_caracteristicas_id', $_id);
        $query = $this->db->get($this->config->item('dbCliente') . $this->config->item('tblCaracteristicaValor'));

        //pre('query: ' . $this->db->last_query());

        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return null;
        }
    }

    function getCaracteristicas($_id) {
        $this->db->where('id', $_id);
        $query = $this->db->get($this->config->item('dbCliente') . $this->config->item('tblCaracteristica'));
        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return null;
        }
    }

    function getCaracteristicasMarcaValor($_caracteristica, $_valor) {
        $this->db->where('marca_caracteristicas_id', $_caracteristica);
        $this->db->where('valor', $_valor);
        $query = $this->db->get($this->config->item('dbCliente') . $this->config->item('tblCaracteristicaValor'));
        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return null;
        }
    }

    function getCaracteristicasValorOrderbyCaracteristica() {
        $this->db->select('cv.*, c.nome');
        $this->db->join($this->config->item('dbCliente') . $this->config->item('tblCaracteristica') . ' as c', 'c.id = cv.marca_caracteristicas_id', 'INNER');
        $this->db->order_by('c.nome', "asc");
        $query = $this->db->get($this->config->item('dbCliente') . $this->config->item('tblCaracteristicaValor') . ' as cv');
        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return null;
        }
    }
    function getCaracteristicasOrderbyNome() {
        $this->db->order_by('nome', "asc");
        $query = $this->db->get($this->config->item('dbCliente') . $this->config->item('tblCaracteristica'));
        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return null;
        }
    }

    function inserir($_dados) {
        $this->db->set($_dados);
        $this->db->insert($this->config->item('dbCliente') . $this->config->item('tblCaracteristicaValor'));
        if ($this->db->affected_rows() > 0) {
            return $this->db->insert_id();
        } else {
            return NULL;
        }
    }

    function atualizar($_id, $_dados) {
        $this->db->where('id', $_id);
        $this->db->set($_dados);
        $this->db->update($this->config->item('dbCliente') . $this->config->item('tblCaracteristicaValor'));
        if ($this->db->affected_rows() > 0) {
            return $_id;
        } else {
            return NULL;
        }
    }

    public function combo($_id, $mensagem='Selecionar') {
        $result = $this->getCaracteristicasValor($_id);

        if ($result != null) {
            $option = array();
            $option[''] = $mensagem;
            foreach ($result as $value) {
                $option[$value->id] = $value->valor;
            }
            return $option;
        }
        return null;
    }

    public function comboCara() {
        $result = $this->getCaracteristicasOrderbyNome();

        if ($result != null) {
            $option = array();
            $option[''] = 'Selecionar';
            foreach ($result as $value) {
                $option[$value->id] = $value->nome;
            }
            return $option;
        }
        return null;
    }

}